// src/services/OnlineTranslator.ts
import { type Translator,type  TranslationResponse } from './Translator';

export class XxApiTranslator implements Translator {
  valid(): boolean {
return true;  }
  getName(): string {
    return '小小api翻译(ed等词形变化的词查询不支持)';
  }
  async translate(text: string, _: string): Promise<TranslationResponse|null> {
    const response = await fetch(`https://v2.xxapi.cn/api/englishwords?word=`+text);
    const data = await response.json();
    console.log("translate res:",data)
    if(data.code!=200) {
      return  null
    }
    let translations = data.data.translations;
    let result = ''
    for(let item of translations) {
      result+=(item.pos+"."+item.tran_cn)
    }
    return {
      translation: result
    };
  }
}